package dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import dao.MerchantDao;
import pojo.Merchant;

@Repository
public class MerchantDaoImpl implements MerchantDao {
	@PersistenceContext(name="un")
	private EntityManager manager; 
	
	@Override
	public Merchant login(Merchant merchant) {
		String ql="select m from Merchant m where m.name=? and m.password = ?";
		System.out.println(merchant.getPassword());
		Merchant merchant_result = (Merchant) manager.createQuery(ql).setParameter(1, merchant.getName()).setParameter(2, merchant.getPassword()).getSingleResult();
		return merchant;
	}

	@Override
	public Merchant add(Merchant merchant) {
		manager.persist(merchant);
		return merchant;
	}

	@Override
	public Merchant update(Merchant merchant) {
		Merchant m = manager.find(Merchant.class, merchant.getId());
		m = merchant;
		manager.clear();
		Merchant mm = manager.merge(m);
		return mm;
	}

}
